package com.example.yichuangyun.dialog;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.GridLayoutManager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.PopupCompanyAreaLayoutBinding;
import com.example.yichuangyun.model.company.AreaModel;
import com.example.yichuangyun.mvvm.ui.adapter.LocationAdapter;
import com.example.yichuangyun.service.MyPhoneService;
import com.example.yichuangyun.ui.adapter.EmptyRecyclerCommonAdapter;
import com.example.yichuangyun.ui.adapter.RecyclerViewHolder;
import com.hjq.permissions.XXPermissions;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("NotifyDataSetChanged")
public abstract class AreaScreenPopupWindow extends PopupWindow implements LocationAdapter.OnItemClickListener {

    private PopupCompanyAreaLayoutBinding binding;
    private final LocationAdapter adapter;
    private final LocationAdapter adapterNearby;
    private final List<String> regAddressIds = new ArrayList<>();
    private final List<AreaModel> nearbyData = new ArrayList<>();
    private final List<AreaModel> data;
    /**
     * 0附近 1行政区
     */
    private int flag;

    private String distance = null;
    private String distanceName;

    @SuppressLint("NotifyDataSetChanged")
    public AreaScreenPopupWindow(Context context, List<AreaModel> data) {
        this.data = data;
        binding = PopupCompanyAreaLayoutBinding.inflate(LayoutInflater.from(context));
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow的焦点
        this.setFocusable(true);
        setContentView(binding.getRoot());
        nearbyData.add(new AreaModel("500m", "0.5"));
        nearbyData.add(new AreaModel("1km", "1"));
        nearbyData.add(new AreaModel("3km", "3"));
        nearbyData.add(new AreaModel("5km", "5"));
        nearbyData.add(new AreaModel("10km", "10"));
        binding.recyclerViewNearby.setLayoutManager(new GridLayoutManager(context, 3));
        adapterNearby = new LocationAdapter(context, 0, this);
        binding.recyclerViewNearby.setAdapter(adapterNearby);
        adapterNearby.submitList(nearbyData);

        binding.recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        adapter = new LocationAdapter(context, 1, this);
        binding.recyclerView.setAdapter(adapter);
        adapter.submitList(data);

        binding.tv1.setOnClickListener(view -> {
            binding.tv1.setTextColor(0xFF426FFF);
            binding.tv1.setBackgroundColor(0xFFFFFFFF);
            binding.tv2.setTextColor(0xFF4A4A4A);
            binding.tv2.setBackgroundColor(0xFFF4F6FA);
            binding.tvNearbyText.setVisibility(View.VISIBLE);
            binding.recyclerViewNearby.setVisibility(View.VISIBLE);
            binding.tvDistrictText.setVisibility(View.GONE);
            binding.recyclerView.setVisibility(View.GONE);
            for (AreaModel areaModel : data) {
                areaModel.setSelect(false);
            }
            adapter.notifyDataSetChanged();
            regAddressIds.clear();
        });

        binding.tv2.setOnClickListener(view -> {
            binding.tv1.setTextColor(0xFF4A4A4A);
            binding.tv1.setBackgroundColor(0xFFF4F6FA);
            binding.tv2.setTextColor(0xFF426FFF);
            binding.tv2.setBackgroundColor(0xFFFFFFFF);
            binding.tvNearbyText.setVisibility(View.GONE);
            binding.recyclerViewNearby.setVisibility(View.GONE);
            binding.tvDistrictText.setVisibility(View.VISIBLE);
            binding.recyclerView.setVisibility(View.VISIBLE);
            for (AreaModel areaModel : nearbyData) {
                areaModel.setSelect(false);
            }
            adapterNearby.notifyDataSetChanged();
            distance = null;
            distanceName = null;
        });

        binding.includeBtn.view.setOnClickListener(v -> dismiss());
        binding.includeBtn.tvReset.setOnClickListener(v -> {
            reset();
            confirm(regAddressIds, null, distance);
            dismiss();
        });
        binding.includeBtn.tvConfirm.setOnClickListener(v -> {
            regAddressIds.clear();
            if (flag == 1) {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).isSelect()) {
                        regAddressIds.add(data.get(i).getTypeCode());
                        builder.append(data.get(i).getAdminName()).append(",");
                    }
                }
                confirm(regAddressIds, builder.toString().length() > 0 ? builder.substring(0, builder.toString().length() - 1) : "", distance);
            } else {
                confirm(regAddressIds, distanceName, distance);
            }
            dismiss();
        });
    }

    private void reset() {
        for (AreaModel areaModel : data) {
            areaModel.setSelect(false);
        }
        adapter.notifyDataSetChanged();
        regAddressIds.clear();

        for (AreaModel areaModel : nearbyData) {
            areaModel.setSelect(false);
        }
        adapterNearby.notifyDataSetChanged();
        distance = null;
        distanceName = null;
    }

    public abstract void confirm(List<String> regAddressIds, String area, String distance);

    @Override
    public void onItemClick(int flag, AreaModel areaModel) {
        this.flag = flag;
        if (flag == 1) {
            areaModel.setSelect(!areaModel.isSelect());
            distance = null;
            distanceName = null;
            adapter.notifyDataSetChanged();
        } else {
            for (AreaModel model : nearbyData) {
                model.setSelect(false);
            }
            areaModel.setSelect(true);
            distance = areaModel.getDistance();
            distanceName = areaModel.getAdminName();
            regAddressIds.clear();
            adapterNearby.notifyDataSetChanged();
        }
    }

    public void setLocationStatus(String status){
        binding.tvNearbyText.setText(status);
    }
}
